package com.hyl.domain.activity.service.discount;

import com.hyl.domain.activity.adapter.repository.IActivityRepository;
import com.hyl.domain.activity.model.valobj.DiscountTypeEnum;
import com.hyl.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author hyl
 * @description 折扣计算服务抽象类
 * @create 2025-06-17 20:02
 */
@Slf4j
public abstract class AbstractDiscountCalculateService implements IDiscountCalculateService{

    @Resource
    private IActivityRepository activityRepository;
    @Override
    public BigDecimal calculate(String userId, BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount) {
        if (DiscountTypeEnum.TAG.equals(groupBuyDiscount.getDiscountType())){
            boolean isCrowdRange = filterTagId(userId,groupBuyDiscount.getTagId());
            if (!isCrowdRange){
                log.info("折扣优惠拦截计算，用户不在优惠人群标签范围内userI{}",userId);
                return originalPrice;
            }
        }
        return doCalculate(originalPrice,groupBuyDiscount);
    }

    protected abstract BigDecimal doCalculate(BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount);

    private boolean filterTagId(String userId, String tagId) {
        return activityRepository.isTagCrowdRange(tagId, userId);
    }

}
